﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Setting.master" AutoEventWireup="true" CodeFile="Departments.aspx.cs" Inherits="Setting_Departments" %>

<asp:Content ID="Content1" ContentPlaceHolderID="SettingHeader" Runat="Server">
    <link href="../Scripts/jquery-ui-1.8.14/smoothness/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css" />
    <script src="../Scripts/jquery-ui-1.8.14/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>

    <link href="../Scripts/dynatree-1.1.1/skin-vista/ui.dynatree.css" rel="stylesheet" type="text/css" />
    <script src="../Scripts/dynatree-1.1.1/jquery.dynatree.js" type="text/javascript"></script>
    <style type="text/css">
        #treeContainer{border-bottom:dotted 1px #eeeeee;}
        #bToolbar{ padding-top:10px;}
        #dialog-addDept{ display:none; }
    </style>

	<script type="text/javascript">
	    var curDeptID = '0';
	    var curDeptName = '所有部门';

	    $(document).ready(function () {

	        $("#tree").dynatree({
	            initAjax: {
	                url: "/setting/do_LoadDept.aspx",
	                data: { id: "0" }
	            },
	            onActivate: function (node) {
	                curDeptID = node.data.key;
	                curDeptName = node.data.title;
	            },
	            onLazyRead: function (node) {
	                node.appendAjax({ url: "/setting/do_LoadDept.aspx",
	                    data: { "id": node.data.key }
	                });
	            }
	        });
	    });

	    function AddDept() {
	        $("#DeptID").val("");
	        $("#DeptName").val("");
	        $("#ParentDeptID").val(curDeptID);
	        $("#ParentDeptName").val(curDeptName);

	        $("#btnSaveContinue").css("display", ""); 

	        $("#dialog-addDept").dialog({
	            modal: true
	        });
	    }

	    function doSaveDept() {
	        //validate
	        if ($("#DeptName").val() == "") {
	            alert("部门名称不能为空!");
	            return;
            }
            //submit
	        $.post("do_SaveDept.aspx", { deptID: $("#DeptID").val(), deptName: $("#DeptName").val(), parentID: $("#ParentDeptID").val() },
			function (data) {
				if (data == "ok") {
				    window.location.reload();
				}
				else
					alert("保存失败!");
			});
        }

	    function EditDept() {
	        $("#DeptID").val(curDeptID);
	        $("#DeptName").val(curDeptName);

	        $.getJSON("do_GetDeptInfo.aspx", { id: curDeptID }, function (jsonData) {
	            $("#ParentDeptID").val(jsonData[0].parentID);
	            $("#ParentDeptName").val(jsonData[0].parentName);
	        });

	        $("#btnSaveContinue").css("display","none"); 

	        $("#dialog-addDept").dialog({
	            modal: true
	        });
	    }

	    function DelDept() {
	        if (curDeptID == '0') {
	            alert('根部门不能被删除。');
	        }
	        else if (confirm('删除该部门，其下属部门也将被删除，部门中的人员将被移动到根部门下。\r\n\r\n您确认要删除吗？')) {
	            $.get("do_DelDept.aspx", { delid: curDeptID },
					  function (data) {
					      if (data == "ok") {
					          $("#tree").dynatree("getTree").getNodeByKey(curDeptID).remove();
					      }
					      else
					          alert("删除失败!");
					  });
	        }
	    }
	</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="SettingContent" Runat="Server">
    <div class="navTitle"><a href="#">系统设置</a> > 部门</div>
    <div id="treeContainer">
        <div id="tree"></div>
    </div>
    <div id="bToolbar"><input type="button" value="添加" onclick="AddDept()" />&nbsp;<input type="button" value="修改" onclick="EditDept()" />&nbsp;<input type="button" value="删除"  onclick="DelDept()"/></div>

    <div id="dialog-addDept" title="添加部门"> 
        <%--<form id="deptForm" name="deptForm"  method="post" action="do_SaveDept.aspx" ></form>--%>
	    <table cellpadding="3"  align="center">
            <tr><td>部门名称</td><td><input type="text" id="DeptName" name="DeptName" /><input type="hidden" id="DeptID" name="DeptID" /></td></tr>
            <tr><td>简称</td><td><input type="text" id="DeptShortName" name="DeptShortName" /></td></tr>
            <tr><td>上级部门</td><td><input type="text" id="ParentDeptName" name="ParentDeptName" /><input type="hidden" id="ParentDeptID" name="ParentDeptID" /><input type="button" value="..." /></td></tr>
            <tr><td></td><td><input type="button" id="btnSave" value="保存" onclick="doSaveDept()" />&nbsp;<input type="button" id="btnSaveContinue" value="保存并继续" onclick="doAddDept()" /></td></tr>
             <tr><td></td><td><span id="tipSpan"></span></td></tr>
        </table>
    </div> 
</asp:Content>

